﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.BandedGrid;
using Security.ERP;
using Common.Base;
using Common.Class;
using Extra.ERP;
using Common;
using DevExpress.Utils;

namespace Security.Security
{
    public class xucGroup : xucBaseBasic
    {
        private IContainer components = null;

        public event AddedEventHander Added;

        public xucGroup()
        {
            this.InitializeComponent();
            base.ucToolBar.SetInterface();
            base.ucToolBar.bbiPermision.Visibility = BarItemVisibility.Always;
            base.ucToolBar.bbiPermision.Enabled = false;
        }

        protected override void Add()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUserGroup") == "OK")
            {
                if (!MyRule.AllowAdd)
                {
                    MyRule.Notify();
                }
                else
                {
                    xfmGroupAdd add = new xfmGroupAdd(Actions.Add);
                    add.Added += new xfmGroupAdd.AddedEventHander(this.frm_Added);
                    add.ShowDialog();
                }
            }
        }

        public void AddRow(SYS_GROUP Item)
        {
            AdvBandedGridView gbList = base.gbList;
            int focusedRowHandle = gbList.FocusedRowHandle;
            gbList.AddNewRow();
            focusedRowHandle = gbList.FocusedRowHandle;
            gbList.SetRowCellValue(focusedRowHandle, "Active", Item.Active);
            gbList.SetRowCellValue(focusedRowHandle, "Group_ID", Item.Group_ID);
            gbList.SetRowCellValue(focusedRowHandle, "Group_Name", Item.Group_Name);
            gbList.SetRowCellValue(focusedRowHandle, "Description", Item.Description);
            gbList.SetRowCellValue(focusedRowHandle, "Active", Item.Active);
            gbList.UpdateCurrentRow();
        }

        protected override void bbiPermision_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Permision();
        }

        public override void Change()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUserGroup") == "OK")
            {
                if (!MyRule.AllowAccess)
                {
                    MyRule.Notify();
                }
                else
                {
                    SYS_GROUP item = new SYS_GROUP();
                    object cellValue = base.GetCellValue(base.m_RowClickEventArgs.RowIndex, "Group_ID");
                    if (cellValue != null)
                    {
                        base.CreateWaitDialog();
                        base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                        if (item.Get(cellValue.ToString()) != "OK")
                        {
                            this.DoHide();
                            XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else
                        {
                            this.DoHide();
                            xfmGroupAdd add = new xfmGroupAdd(Actions.Update, item);
                            add.Updated += new xfmGroupAdd.UpdatedEventHander(this.frm_Updated);
                            add.ShowDialog();
                        }
                    }
                }
            }
        }

        public override void Delete()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUserGroup") == "OK")
            {
                if (!MyRule.AllowDelete)
                {
                    MyRule.Notify();
                }
                else if (!ClsOption.System2.IsQuestion || (XtraMessageBox.Show("Bạn c\x00f3 muốn x\x00f3a kh\x00f4ng?", "Th\x00f4ng b\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No))
                {
                    object cellValue;
                    base.CreateWaitDialog();
                    base.SetWaitDialogCaption("Đang x\x00f3a...");
                    string str = "";
                    bool flag = false;
                    AdvBandedGridView gbList = base.gbList;
                    int[] selectedRows = gbList.GetSelectedRows();
                    SYS_GROUP sys_group = new SYS_GROUP();
                    for (int i = selectedRows.Length; i > 0; i--)
                    {
                        flag = true;
                        cellValue = base.GetCellValue(selectedRows[i - 1], "Group_ID");
                        if (cellValue != null)
                        {
                            SYS_LOG.Insert("Vai Tr\x00f2 Người D\x00f9ng", "Xo\x00e1", cellValue.ToString());
                            if (cellValue.ToString().ToLower() == "admin")
                            {
                                XtraMessageBox.Show("Nh\x00f3m người d\x00f9ng kh\x00f4ng n\x00e0y kh\x00f4ng được xo\x00e1.", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                str = sys_group.Delete(cellValue.ToString());
                                if (str == "OK")
                                {
                                    gbList.DeleteRow(selectedRows[i - 1]);
                                }
                                else if (str != "OK")
                                {
                                    MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                        }
                    }
                    this.DoHide();
                    if (!flag && (gbList.DataSource != null))
                    {
                        RowClickEventArgs args = new RowClickEventArgs((gbList == null) ? -1 : gbList.FocusedRowHandle, (gbList.FocusedColumn == null) ? -1 : gbList.FocusedColumn.ColumnHandle, (gbList.FocusedColumn == null) ? "" : gbList.FocusedColumn.FieldName);
                        base.m_RowClickEventArgs = args;
                        cellValue = null;
                        cellValue = base.GetCellValue(args.RowIndex, "Group_ID");
                        if (cellValue != null)
                        {
                            SYS_LOG.Insert("Vai Tr\x00f2 Người D\x00f9ng", "Xo\x00e1", cellValue.ToString());
                            if (cellValue.ToString().ToLower() == "admin")
                            {
                                XtraMessageBox.Show("Nh\x00f3m người d\x00f9ng kh\x00f4ng n\x00e0y kh\x00f4ng được xo\x00e1.", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                base.CreateWaitDialog();
                                base.SetWaitDialogCaption("Đang x\x00f3a...");
                                str = sys_group.Delete(cellValue.ToString());
                                if (str == "OK")
                                {
                                    gbList.DeleteRow(args.RowIndex);
                                }
                                else if (str != "OK")
                                {
                                    MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                }
                                this.DoHide();
                            }
                        }
                    }
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override bool ExportPermision()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUserGroup") != "OK")
            {
                return false;
            }
            if (!MyRule.AllowExport)
            {
                MyRule.Notify();
                return false;
            }
            return true;
        }

        private void frm_Added(object sender, SYS_GROUP e)
        {
            this.AddRow(e);
            this.RaiseAddedEventHander(e);
        }

        private void frm_Updated(object sender, SYS_GROUP Item)
        {
            this.UpdateRow(Item, base.m_RowClickEventArgs);
        }

        protected override void gbList_MouseDown(object sender, MouseEventArgs e)
        {
            AdvBandedGridView view = (AdvBandedGridView)sender;
            RowClickEventArgs args = new RowClickEventArgs((view == null) ? -1 : view.FocusedRowHandle, (view.FocusedColumn == null) ? -1 : view.FocusedColumn.ColumnHandle, (view.FocusedColumn == null) ? "" : view.FocusedColumn.FieldName);
            base.m_RowClickEventArgs = args;
            object cellValue = base.GetCellValue(args.RowIndex, "Group_ID");
            base.DisableMenu(false);
            if (cellValue == null)
            {
                base.DisableMenu(true);
            }
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(xucGroup));
            base.SuspendLayout();
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucGroup";
            base.Size = new Size(0x2f9, 0x202);
            base.ResumeLayout(false);
        }

        protected override void List_Init(AdvBandedGridView dt)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string fieldName = dt.Columns[i].FieldName;
                if (fieldName == null)
                {
                    goto Label_03CC;
                }
                if (!(fieldName == "Group_ID"))
                {
                    if (fieldName == "Group_Name")
                    {
                        goto Label_013E;
                    }
                    if (fieldName == "Description")
                    {
                        goto Label_021C;
                    }
                    if (fieldName == "Active")
                    {
                        goto Label_0311;
                    }
                    goto Label_03CC;
                }
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = "M\x00e3";
                }
                else
                {
                    dt.Columns[i].Caption = "Role_Id";
                }
                dt.Columns[i].Width = 90;
                continue;
            Label_013E:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = "Vai tr\x00f2";
                }
                else
                {
                    dt.Columns[i].Caption = "Role_Name";
                }
                dt.Columns[i].Width = 180;
                continue;
            Label_021C:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                dt.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                dt.Columns[i].DisplayFormat.FormatString = ClsOption.Number.FormatString;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = "Diễn giải";
                }
                dt.Columns[i].Width = 180;
                continue;
            Label_0311:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = "K\x00edch hoạt";
                }
                dt.Columns[i].Width = 80;
                continue;
            Label_03CC:
                dt.Columns[i].Visible = false;
            }
        }

        public override void Permision()
        {
            object cellValue = base.GetCellValue(base.m_RowClickEventArgs.RowIndex, "Group_ID");
            if (cellValue != null)
            {
                base.CreateWaitDialog();
                base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                SYS_GROUP sys_group = new SYS_GROUP();
                if (sys_group.Get(cellValue.ToString()) != "OK")
                {
                    this.DoHide();
                    XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    this.DoHide();
                    new xfmPermision().ShowDialog();
                }
            }
        }

        protected override void Print()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUserGroup") == "OK")
            {
                if (!MyRule.AllowPrint)
                {
                    MyRule.Notify();
                }
                else
                {
                    base.Print();
                }
            }
        }

        private void RaiseAddedEventHander(SYS_GROUP item)
        {
            if (this.Added != null)
            {
                this.Added(this, item);
            }
        }

        public override void ReLoad()
        {
            base.CreateWaitDialog();
            base.SetWaitDialogCaption("Đang nạp dữ liệu...");
            base.gcList.DataSource = new SYS_GROUP().GetList();
            base.SetWaitDialogCaption("Đang nạp cấu h\x00ecnh...");
            this.List_Init(base.gbList);
            base.SetWaitDialogCaption("Nạp quyền sử dụng...");
            MyRule.Get(MyLogin.RoleId, "bbiUserGroup");
            if (!MyRule.AllowPrint)
            {
                base.ucToolBar.bbiPrint.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUserGroup");
            if (!MyRule.AllowExport)
            {
                base.ucToolBar.bbiExport.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUserGroup");
            if (!MyRule.AllowAdd)
            {
                base.ucToolBar.bbiAdd.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUserGroup");
            if (!MyRule.AllowDelete)
            {
                base.ucToolBar.bbiDelete.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUserGroup");
            if (!MyRule.AllowEdit)
            {
                base.ucToolBar.bbiEdit.Visibility = BarItemVisibility.Never;
            }
            base.SetWaitDialogCaption("Đ\x00e3 xong...");
            this.DoHide();
        }

        public void UpdateRow(SYS_GROUP Item, RowClickEventArgs e)
        {
            AdvBandedGridView gbList = base.gbList;
            int rowIndex = e.RowIndex;
            gbList.SetRowCellValue(rowIndex, "Active", Item.Active);
            gbList.SetRowCellValue(rowIndex, "Group_ID", Item.Group_ID);
            gbList.SetRowCellValue(rowIndex, "Group_Name", Item.Group_Name);
            gbList.SetRowCellValue(rowIndex, "Description", Item.Description);
            gbList.SetRowCellValue(rowIndex, "Active", Item.Active);
            gbList.UpdateCurrentRow();
        }

        private void xucCustomerType_Load(object sender, EventArgs e)
        {
        }

        public delegate void AddedEventHander(object sender, SYS_GROUP item);
    }
}
